/**
 * @author Glan.duanyj
 * @date 2014-05-12
 * @project rest_demo
 */
package com.jsz.peini.sms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsz.peini.sms.ucpaas.restDemo.client.JsonReqClient;

public class SmsSend {
	private static final Logger logger = LoggerFactory.getLogger(SmsSend.class);
	//账户id
	private static final String accountSid = "1f76aa10c631e4fae498a0d18e24118a";
	//账户token
	private static final String authToken = "756eb8a5662c140d96e1c9097a4b7385";
	//应用id
	private static final String appId = "f790ef239fb9435da45aa5357e96863e";
	//短信模板id
	private static final String templateId = "33749"; 
	
	/**
	 * @param userName  用户手机号
	 * @param param 验证码参数
	 */
	public static String sendTemplateSMS(String userName,String param){
		String result=null;
		try {
			JsonReqClient josnreq= new JsonReqClient();
			result=josnreq.templateSMS(accountSid, authToken, appId, templateId, userName, param);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	public static String sendSMSByTask(String userName,String param,String smstype){
		String result=null;
		try {
			JsonReqClient josnreq= new JsonReqClient();
			result=josnreq.templateSMS(accountSid, authToken, appId, smstype, userName, param);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
//	public static boolean sendMessage(String userName,String param,int smstype){
//		boolean isSend = false;
//		String result = SmsSend.sendTemplateSMS(userName, param);
//		logger.info("短信返回结果:"+result);
//		JSONObject object = JSON.parseObject(result);
//		JSONObject data = (JSONObject) object.get("resp");
//		String respCode = data.getString("respCode");
//		//发送成功
//		if(respCode.equals("000000")){
//			isSend = true;
//		}
//		return isSend;
//	 }
	
	public static String sendMessage(String userName,String param,int smstype){
		String result = SmsSend.sendTemplateSMS(userName, param);
		logger.info("短信返回结果:"+result);
		JSONObject object = JSON.parseObject(result);
		JSONObject data = (JSONObject) object.get("resp");
		String respCode = data.getString("respCode");
		return respCode;
	 }
	
	public static String sendByTask(String userName,String param,String smstype){
		String result = SmsSend.sendSMSByTask(userName, param,smstype);
		JSONObject object = JSON.parseObject(result);
		JSONObject data = (JSONObject) object.get("resp");
		String respCode = data.getString("respCode");
		return respCode;
	}
	
	
	/**
	 * 测试说明 参数顺序，请参照各具体方法参数名称
	 * 参数名称含义，请参考rest api 文档
	 * @author Glan.duanyj
	 * @date 2014-06-30
	 * @param params[]
	 * @return void
	 * @throws IOException 
	 * @method main
	 */
//	public String smsSend(String userName,String smsCode){
//		String  accountSid="1f76aa10c631e4fae498a0d18e24118a";
//		String token="756eb8a5662c140d96e1c9097a4b7385";
//		String appId="f790ef239fb9435da45aa5357e96863e";
//		String templateId="33749";
//		String to=userName;
//		String para=smsCode;
//		String smsSendReturn = sendTemplateSMS(accountSid,token,appId, templateId,to,para);
//		logger.info(smsSendReturn);
//		return smsSendReturn;
//	}
	
//	public static void main(String[] args) throws IOException {
//		SmsSend re=new SmsSend();
//		re.smsSend("13633293531", "123456");
//	}
//	 public static void main(String[] args){
//	 String ss = "{\"resp\":{\"respCode\":\"000000\",\"templateSMS\":{\"createDate\":\"20161209182011\",\"smsId\":\"ee7677284b5eca2fd6a4f69dcc511609\"}}}";
//	 logger.info("短信返回结果:"+ss);
//	 JSONObject object = JSON.parseObject(ss);
//    JSONObject data = (JSONObject) object.get("resp");
//    String dd = data.getString("respCode");
//	 logger.info("短信返回结果:"+dd);
//}
}
